# base_api.py
import json
import requests
from aaa.utils.config_util import ConfigUtil


class BaseAPI:
    def __init__(self, session=None, config_file='config.ini'):
        self.session = session or requests.Session()
        self.config = ConfigUtil(config_file)
        self.BASE_URL = self.config.get_base_url()

    def _handle_response(self, response, action):
        try:
            response.encoding = 'utf-8'
            body = json.loads(response.text)
            print(f"{action}响应：{body}")
            if response.status_code == 200 and body.get('code') == 100:
                print(f"{action}成功")
                return True
            else:
                print(f"{action}失败：{body.get('msg', '未知错误')}")
                return False
        except json.JSONDecodeError:
            print(f"服务器未返回JSON格式，原始响应：{response.text}")
            return False
